package com.cbn.ibcp.jishiting.gs.cms.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cbn.ibcp.common.annotation.DataSource;
import com.cbn.ibcp.common.enums.DataSourceType;
import com.cbn.ibcp.jishiting.gs.cms.domain.NodeVideo;
import com.cbn.ibcp.jishiting.gs.cms.mapper.NodeVideoMapper;
import com.cbn.ibcp.jishiting.gs.cms.service.INodeVideoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Slf4j
@Service
@RequiredArgsConstructor
public class NodeVideoServiceImpl implements INodeVideoService {


    private final NodeVideoMapper nodeVideoMapper;


    @DataSource(DataSourceType.gs_cms)
    @Override
    public List<NodeVideo> getListByVideoIdList(List<Long> videoIds, Long nodeId) {
        if (CollUtil.isEmpty(videoIds) || Objects.isNull(nodeId)) {
            return null;
        }
        List<String> videoIdStrList = videoIds.stream().map(StrUtil::toStringOrNull).collect(Collectors.toList());
        LambdaQueryWrapper<NodeVideo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(NodeVideo::getVideoId, videoIdStrList);
        lambdaQueryWrapper.eq(NodeVideo::getNodeIdx, nodeId);
        return nodeVideoMapper.selectList(lambdaQueryWrapper);
    }


    @DataSource(DataSourceType.gs_cms)
    @Override
    public NodeVideo getByVideoId(Long videoId, Long nodeId) {
        if (Objects.isNull(videoId) || Objects.isNull(nodeId)) {
            return null;
        }
        LambdaQueryWrapper<NodeVideo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NodeVideo::getVideoId, videoId);
        queryWrapper.eq(NodeVideo::getNodeIdx, nodeId);
        List<NodeVideo> nodeVideos = nodeVideoMapper.selectList(queryWrapper);
        return CollUtil.isNotEmpty(nodeVideos) ? nodeVideos.get(0) : null;
    }

    @DataSource(DataSourceType.gs_cms)
    @Override
    public NodeVideo getByAssetId(String assetId, Long nodeId) {
        if (StrUtil.isBlank(assetId)) {
            return null;
        }
        LambdaQueryWrapper<NodeVideo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NodeVideo::getAssetId, assetId);
        queryWrapper.eq(NodeVideo::getNodeIdx, nodeId);
        List<NodeVideo> nodeVideos = nodeVideoMapper.selectList(queryWrapper);
        return CollUtil.isNotEmpty(nodeVideos) ? nodeVideos.get(0) : null;
    }

    @DataSource(DataSourceType.gs_cms)
    @Override
    public Integer getTotalCreateInjectStatistic(String orgCode, Date startTime, Date endTime, Integer injectStatus) {
        log.info("getTotalCreateInjectStatistic param,orgCode:{},startTime:{},endTime:{}", orgCode, startTime, endTime);
        return nodeVideoMapper.selectTotalCreateInjectCount(orgCode, startTime, endTime, injectStatus);
    }

    @DataSource(DataSourceType.gs_cms)
    @Override
    public Integer getTotalCreateInjectFailureStatistic(String orgCode, Date startTime, Date endTime) {
        log.info("getTotalCreateInjectFailureStatistic param,orgCode:{},startTime:{},endTime:{}", orgCode, startTime, endTime);
        return nodeVideoMapper.selectTotalCreateInjectFailureCount(orgCode, startTime, endTime);
    }

    @DataSource(DataSourceType.gs_cms)
    @Override
    public Integer getTotalUpdateInjectStatistic(String orgCode, Date startTime, Date endTime, Integer injectStatus) {
        log.info("getTotalUpdateInjectStatistic param,orgCode:{},startTime:{},endTime:{}", orgCode, startTime, endTime);
        return nodeVideoMapper.selectTotalUpdateInjectCount(orgCode, startTime, endTime, injectStatus);
    }

    @DataSource(DataSourceType.gs_cms)
    @Override
    public Integer getTotalUpdateInjectFailureStatistic(String orgCode, Date startTime, Date endTime) {
        log.info("getTotalUpdateInjectFailureStatistic param,orgCode:{},startTime:{},endTime:{}", orgCode, startTime, endTime);
        return nodeVideoMapper.selectTotalUpdateInjectFailureCount(orgCode, startTime, endTime);
    }
}
